{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'keras'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-c676d3b25750>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodels\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mload_model\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpreprocessing\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mimage\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mos\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mlistdir\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'keras'"
     ]
    }
   ],
   "source": [
    "from keras.models import load_model\n",
    "from keras.preprocessing import image\n",
    "from os import listdir\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ROWS = 256\n",
    "COLS = 256"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "CLASS_NAMES = sorted(listdir('images'))\n",
    "\n",
    "model = load_model('birds-inceptionv3.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict(fname):\n",
    "    img = image.load_img(fname, target_size=(ROWS, COLS))\n",
    "    img_tensor = image.img_to_array(img) # (height, width, channels)\n",
    "    # (1, height, width, channels), add a dimension because the model expects this shape:\n",
    "    # (batch_size, height, width, channels)\n",
    "    img_tensor = np.expand_dims(img_tensor, axis=0) \n",
    "    img_tensor /= 255. # model expects values in the range [0, 1]\n",
    "    prediction = model.predict(img_tensor)[0]\n",
    "    best_score_index = np.argmax(prediction)\n",
    "    bird = CLASS_NAMES[best_score_index] # retrieve original class name\n",
    "    print(\"Prediction: %s (%.2f%%)\" % (bird, 100*prediction[best_score_index]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prediction: 067.Anna_Hummingbird (98.76%)\n",
      "Prediction: 196.House_Wren (47.01%)\n",
      "Prediction: 071.Long_tailed_Jaeger (37.12%)\n"
     ]
    }
   ],
   "source": [
    "predict('test-birds/annas_hummingbird_sim_1.jpg')\n",
    "predict('test-birds/house_wren.jpg')\n",
    "predict('test-birds/canada_goose_1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# interactive user input\n",
    "while True:\n",
    "    fname = input(\"Enter filename: \")\n",
    "    if(len(fname) > 0):\n",
    "        try:\n",
    "            predict(fname)\n",
    "        except Exception as e:\n",
    "            print(\"Error loading image: %s\" % e)\n",
    "    else:\n",
    "        break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
